package com.example.exam;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CalendarView;
import android.widget.ExpandableListView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private Switch switch1;
    private SharedPreferences sp;
    private ExpandableListView expand;
    private Spinner spinner;
    private TextView textNetWork;
    private TextView textTime;
    private CalendarView calendar;

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel("com.example.exam", "MainActivity", NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
        Notification notification = new NotificationCompat.Builder(MainActivity.this,"com.example.exam")
                .setContentTitle("你好")
                .setContentText("tvuhjgvjyhuhyub")
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .build();
        manager.notify(1,notification);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        String[] s = new String[]{Manifest.permission.CALL_PHONE};
        for (int i = 0; i < s.length; i++) {
            int i1 = checkCallingPermission(s[i]);
            if (i1 != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, s, 100);
            }
        }
        sp = getSharedPreferences("sign", MODE_PRIVATE);
        initView();
    }

    private void initView() {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout2, null);
        builder.setView(view);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        switch1 = (Switch) findViewById(R.id.switch1);
        switch1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = switch1.isChecked();
                SharedPreferences.Editor edit = sp.edit();
                edit.putBoolean("switchB", checked);
                edit.commit();
            }
        });
        boolean switchB = sp.getBoolean("switchB", false);
        switch1.setChecked(switchB);
        expand = (ExpandableListView) findViewById(R.id.expand);
        expand.setAdapter(new ExpandAdapter(MainActivity.this));
        spinner = (Spinner) findViewById(R.id.spinner);
        final String[] arr = getResources().getStringArray(R.array.sort);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, arr);
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, "" + i, Toast.LENGTH_SHORT).show();
                if (i == 1) {startActivity(new Intent(MainActivity.this,Main2Activity.class));}
                if (i == 2) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + 13973356624L));
                    startActivity(intent);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        textNetWork = (TextView) findViewById(R.id.textNetWork);
        textNetWork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS));
            }
        });
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                String netWork = new NetWork().getNetWork(MainActivity.this);
                textNetWork.setText(netWork);
            }
        };
        timer.schedule(timerTask, 0, 300);
        textTime = (TextView) findViewById(R.id.textTime);
        TimeDay timeDay = new TimeDay();
        textTime.setText(timeDay.getMinTimeDay(1) + "\n" + timeDay.getTimeDay() + "\n" + timeDay.getMaxTimeDay(1) + "\n");
        calendar = (CalendarView) findViewById(R.id.calendar);
        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
                textTime.setText(i+"年"+(i1+1)+"月"+i2+"日");
            }
        });
    }
}
